package com.wxj.book.controller;

import com.wxj.book.common.Const;
import com.wxj.book.common.ServerResponse;
import com.wxj.book.pojo.Receiver;
import com.wxj.book.pojo.User;
import com.wxj.book.service.ReceiverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ReceiverController {
    @Autowired
    private ReceiverService receiverService;

    /**
     * 预约领取图书
     */
    @RequestMapping("receiver/book")
    @ResponseBody
    public ServerResponse bookReceiver(HttpServletRequest request) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorMessage("预约失败, 请先登录");
        }
        Receiver receiver = receiverService.getByStuNum(user.getStuNum());
        if (receiver != null) {
            return ServerResponse.createByErrorMessage("不能重复预约");
        }
        // 写入数据库
        int result = receiverService.insert(user);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("预约成功");
        }
        return ServerResponse.createByErrorMessage("预约失败");
    }

}
